/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_announce_config_manager.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {

SleAnnounceConfigManager::SleAnnounceConfigManager()
{
    SLE_ASSERT_AND_RETURN(fileName_.empty() == false);
    announceCfg_ = std::make_unique<SleAnnounceParam>();
    sleCfgMgr_ = std::make_unique<SleConfigManager>(fileName_);
    SLE_ASSERT_AND_RETURN_LOG(sleCfgMgr_ != NULL, "create sleCfgMgr_ fail");
    SLE_ASSERT_AND_RETURN(GetConfigFromValues() == SLE_NO_ERROR);
}

SleAnnounceConfigManager::SleAnnounceConfigManager(std::string fileName)
{
    SLE_ASSERT_AND_RETURN(fileName.empty() == false);
    fileName_ = fileName;
    announceCfg_ = std::make_unique<SleAnnounceParam>();
    sleCfgMgr_ = std::make_unique<SleConfigManager>(fileName_);
    SLE_ASSERT_AND_RETURN_LOG(sleCfgMgr_ != NULL, "create sleCfgMgr_ fail");
    SLE_ASSERT_AND_RETURN(GetConfigFromValues() == SLE_NO_ERROR);
}

SleAnnounceConfigManager::~SleAnnounceConfigManager()
{
//    SleErrorCode ret = SetConfigToValues();
//    SLE_ASSERT_AND_RETURN(ret == SLE_NO_ERROR);
//    SLE_ASSERT_AND_RETURN(SetConfigToValues() == SLE_NO_ERROR);
}

SleErrorCode SleAnnounceConfigManager::GetAnnounceConfig(SleAnnounceParam &announceConfig)
{
    SLE_ASSERT_AND_RETURN_RET(announceCfg_ != NULL, SLE_ERR_NOT_READY);
    announceConfig = *announceCfg_.get();
    return SLE_NO_ERROR;
}

SleErrorCode SleAnnounceConfigManager::GetConfigFromValues()
{
    SleConfigValueMap valueMap;
    SleErrorCode ret = sleCfgMgr_->GetValue(valueMap);
    std::string sectionStr = "sle_announce_config";
    SleConfigValue values = valueMap[sectionStr];
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    announceCfg_->handle = sleCfgMgr_->Str2Uint8(values["handle"]);
    announceCfg_->mode = sleCfgMgr_->Str2Uint8(values["mode"]);
    announceCfg_->role = sleCfgMgr_->Str2Uint8(values["role"]);
    announceCfg_->level = sleCfgMgr_->Str2Uint8(values["level"]);
    announceCfg_->annonceIntervalMin = sleCfgMgr_->Str2Uint32(values["annonceIntervalMin"]);
    announceCfg_->annonceIntervalMax = sleCfgMgr_->Str2Uint32(values["annonceIntervalMax"]);
    announceCfg_->channelMap = sleCfgMgr_->Str2Uint8(values["channelMap"]);
    announceCfg_->txPower = sleCfgMgr_->Str2Uint8(values["txPower"]);
    announceCfg_->connectIntervalMin = sleCfgMgr_->Str2Uint16(values["connectIntervalMin"]);
    announceCfg_->connectIntervalMax = sleCfgMgr_->Str2Uint16(values["connectIntervalMax"]);
    announceCfg_->connectLatency = sleCfgMgr_->Str2Uint16(values["connectLatency"]);
    announceCfg_->connectTimeout = sleCfgMgr_->Str2Uint16(values["connectTimeout"]);
    //no include cfg
    announceCfg_->extParam = NULL;
    return SLE_NO_ERROR;
}

SleErrorCode SleAnnounceConfigManager::SetConfigToValues()
{
    SleConfigValue values;
    values["handle"] = std::to_string(announceCfg_->handle);
    values["mode"] = std::to_string(announceCfg_->mode);
    values["role"] = std::to_string(announceCfg_->role);
    values["level"] = std::to_string(announceCfg_->level);
    values["annonceIntervalMin"] = std::to_string(announceCfg_->annonceIntervalMin);
    values["annonceIntervalMax"] = std::to_string(announceCfg_->annonceIntervalMax);
    values["channelMap"] = std::to_string(announceCfg_->channelMap);
    values["txPower"] = std::to_string(announceCfg_->txPower);
    values["connectIntervalMin"] = std::to_string(announceCfg_->connectIntervalMin);
    values["connectIntervalMax"] = std::to_string(announceCfg_->connectIntervalMax);
    values["connectLatency"] = std::to_string(announceCfg_->connectLatency);
    values["connectTimeout"] = std::to_string(announceCfg_->connectTimeout);

    std::string sectionStr = "sle_announce_config";
    SleConfigValueMap valueMap;
    valueMap[sectionStr] = values;
    SleErrorCode ret = sleCfgMgr_->SetValue(valueMap);
    SLE_ASSERT_AND_RETURN_RET(ret == SLE_NO_ERROR, ret);
    return SLE_NO_ERROR;
}

} // SleStandard
} // NearLink
} // OHOS